﻿package com.boyaa.shells.tool
{
	import flash.display.MovieClip;
	import flash.display.SimpleButton;
	import flash.display.Sprite;
	import flash.events.Event;
	import flash.events.EventDispatcher;
	import flash.events.MouseEvent;
	import flash.geom.Rectangle;
	
	/**
	 * 滑动条控制器
	 * @author xishuai
	 */
	public class SliderContorl extends EventDispatcher
	{
		private var _display:MovieClip = null;			// 显示对象
		
		private var _upBtn:SimpleButton = null;			// 上按钮
		private var _downBtn:SimpleButton = null;		// 下按钮
		
		private var _track:MovieClip = null;			// 移动轨道
		private var _rollBar:MovieClip = null;			// 滚动条
		private var _top:MovieClip = null;				// 滚动条上部
		private var _bottom:MovieClip = null;			// 滚动条下部
		private var _middle:MovieClip = null;			// 滚动条中部
		private var _barLine:MovieClip = null;			// 中间线
		
		private var _hei:Number = 0;					// 滚动条高
		private var _wid:Number = 0;					// 滚动条宽
		private var _minScrollPosition:Number = 0;		// 最小显示行数
		private var _maxScrollPosition:Number = 0;		// 最大显示行数
		private var _position:int = 0;					// 当前位置
		
		private var _style:int = 0;						// 样式 0竖 1横
		
		private const UP_RATE:Number = 0.1;				// 滚动率
		public const SCROLL:String = "scroll";			// 滚动事件
		
		/**
		 * 控制器
		 * @param	mc 显示对象
		 * @param	height 长度
		 * @param	minPos 最小行数
		 * @param	maxPos 最大行数
		 * @param	style 样式
		 */
		public function SliderContorl(mc:MovieClip,height:Number,minPos:Number,maxPos:Number,style:int = 0)
		{
			_display = mc;
			
			if (height > 0)
			{
				_hei = height;
			}
			
			_minScrollPosition = minPos;
			_maxScrollPosition = maxPos;
			
			_style = style;
			
			init();
		}
		
		/**
		 * 初始化
		 */
		private function init():void
		{
			_upBtn = _display.upBtn;
			_downBtn = _display.downBtn;
			_track = _display.track;
			_rollBar = _display.rollBar;
			_top = _rollBar.top;
			_bottom = _rollBar.bottom;
			_middle = _rollBar.middle;
			_barLine = _rollBar.barLine;
			setRollBar("out");
			setStyle();
			addEvent();
		}
		
		/**
		 * 添加监听
		 */
		private function addEvent():void
		{
			_rollBar.addEventListener(MouseEvent.MOUSE_DOWN, downRollBar);
			_rollBar.addEventListener(MouseEvent.MOUSE_OUT, outRollBar);
			
			
			_upBtn.addEventListener(MouseEvent.CLICK, upBtnDown);
			_downBtn.addEventListener(MouseEvent.CLICK, downBtnDown);
			
		}
		
		private function onRollBarChange(e:Event):void 
		{
			refreshTest();
			dispatchEvent(new Event(SCROLL));
		}
		
		private function refreshTest():void
		{
			_position = (_rollBar.y - _upBtn.height) / (_track.height - _rollBar.height) * 100;
			if (_position > 95)
			{
				_position = 100;
			}
			if (_position < 0)
			{
				_position = 0;
			}
		}
		
		/**
		 * 移除控制器
		 */
		public function remove():void
		{
			_rollBar.removeEventListener(MouseEvent.MOUSE_DOWN, downRollBar);
			_rollBar.removeEventListener(MouseEvent.MOUSE_OUT, outRollBar);
			_upBtn.removeEventListener(MouseEvent.CLICK, upBtnDown);
			_downBtn.removeEventListener(MouseEvent.CLICK, downBtnDown);
		}
		
		/**
		 * 按下
		 * @param	e
		 */
		private function downBtnDown(e:MouseEvent):void 
		{
			_rollBar.y += _track.height * UP_RATE;
			if (_rollBar.y >= _downBtn.y-_rollBar.height)
			{
				_rollBar.y = _downBtn.y-_rollBar.height;
			}
			onRollBarChange(null);
		}
		
		/**
		 * 按上
		 * @param	e
		 */
		private function upBtnDown(e:MouseEvent):void 
		{
			_rollBar.y -= _track.height * UP_RATE;
			if (_rollBar.y <= _upBtn.height)
			{
				_rollBar.y = _upBtn.height;
			}
			onRollBarChange(null);
		}
		
		/**
		 * 按滚动条
		 * @param	e
		 */
		private function downRollBar(e:MouseEvent):void 
		{
			_display.stage.addEventListener(MouseEvent.MOUSE_UP, upRollBar);
			var bounds:Rectangle = new Rectangle( -1, _upBtn.height, 0, _track.height-_rollBar.height);
			_rollBar.startDrag(false, bounds);
			setRollBar("down");
			_rollBar.addEventListener(Event.ENTER_FRAME, onRollBarChange);
		}
		
		/**
		 * 放滚动条
		 * @param	e
		 */
		private function upRollBar(e:MouseEvent):void 
		{
			_display.stage.removeEventListener(MouseEvent.MOUSE_UP, upRollBar);
			_rollBar.removeEventListener(Event.ENTER_FRAME, onRollBarChange);
			_rollBar.stopDrag();
			setRollBar("out");
		}
		
		/**
		 * 移出滚动条
		 * @param	e
		 */
		private function outRollBar(e:MouseEvent):void 
		{
			setRollBar("out");
		}
		
		private function setRollBar(frame:String):void
		{
			_top.gotoAndStop(frame);
			_middle.gotoAndStop(frame);
			_bottom.gotoAndStop(frame);
		}
		
		/**
		 * 设置样式
		 */
		private function setStyle():void
		{
			if (_style == 0)
			{
				_track.height = _hei - _upBtn.height - _downBtn.height;
				_downBtn.y = _upBtn.height + _track.height;
				
				var rate:Number = _minScrollPosition / _maxScrollPosition;
				
				if (rate > 1)
				{
					rate = 0;
					_rollBar.visible = false;
				}
				else
				{
					_rollBar.visible = true;
				}
				_middle.height = (_track.height- _top.height - _bottom.height) * rate;
				_bottom.y = _top.height + _middle.height;
				_rollBar.y = _upBtn.height;
				_barLine.y = _middle.y + _middle.height / 2;
			}
		}
		
		public function get minScrollPosition():Number { return _minScrollPosition; }
		
		public function set minScrollPosition(value:Number):void 
		{
			_minScrollPosition = value;
			setStyle();
		}
		
		public function get maxScrollPosition():Number { return _maxScrollPosition; }
		
		public function set maxScrollPosition(value:Number):void 
		{
			_maxScrollPosition = value;
			setStyle();
		}
		
		public function get position():int { return _position; }
		
		public function set position(value:int):void 
		{
			_position = value;
			//_position = (_rollBar.y - _upBtn.height) /(_track.height-_rollBar.height) * 100;
			_rollBar.y = (_track.height - _rollBar.height) * Number(_position/100) + _upBtn.height;
		}
		
		
		
	}
	
}